/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.transformation.editor.controller.editparts.link;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.ConnectionEndpointLocator;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PolylineConnection;

import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.variable.ConnectionVariable;

/**
 * @generated
 * 
 */
public class ConnectionVariableEditpart extends LinkTemplateEditpart {
	private Label sourceRole = new Label();
	private Label targetRole = new Label();

	public ConnectionVariableEditpart(MetaShapeElement model) {
		super(model);
	}

	protected IFigure createFigure() {
		PolylineConnection conn = new PolylineConnection();
		ConnectionVariable cv = (ConnectionVariable) ((MetaLink) getModel())
				.getModelElement();
		ConnectionEndpointLocator targetLocator = new ConnectionEndpointLocator(
				conn, true);
		ConnectionEndpointLocator sourceLocator = new ConnectionEndpointLocator(
				conn, false);
		conn.add(sourceRole, sourceLocator);
		conn.add(targetRole, targetLocator);
		sourceRole.setText(cv.getSourceRole());
		targetRole.setText(cv.getTargetRole());

		return conn;
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		if (ModelPackage.CONNECTION_VARIABLE_SOURCEROLE.equals(propertyName)) {
			sourceRole.setText((String) event.getNewValue());
		} else if (ModelPackage.CONNECTION_VARIABLE_TARGETROLE
				.equals(propertyName)) {
			targetRole.setText((String) event.getNewValue());
		}
	}
}
